package com.woniu.order.controller;

import cn.hutool.core.lang.UUID;
import com.woniu.api.movie.MovieApi;
import com.woniu.common.entity.dto.PaymentNotification;
import com.woniu.common.result.Result;
import com.woniu.common.entity.Order;
import com.woniu.common.entity.vo.OrderVo;
import com.woniu.common.utils.JwtTemplate;
import com.woniu.common.utils.TokenUtils;
import com.woniu.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mane
 * @since 2025-03-29
 */
@RestController
@RequestMapping("/api/orders")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping
    public Result addOrder(@RequestBody Order order) {
        //1 获取token中的userId
        String userId = TokenUtils.getUserIdByToken("userId");
        //2.封装
        int orderId = UUID.fastUUID().toString().hashCode();
        order.setUserId(Integer.valueOf(userId));
        order.setId(orderId);
        orderService.save(order);
        return Result.success("订单添加成功",orderId);
    }

    @GetMapping("/userId/{userId}")
    public Result<List<OrderVo>> getOrdersByUserId(@PathVariable Integer userId) {
        List<OrderVo> orderVos = orderService.getOrdersByUserId(userId);
        return Result.success(orderVos);
    }

    @GetMapping("/{id}")
    public Result<Order> getOrderById(@PathVariable Integer id) {
        Order order = orderService.getById(id);
        return Result.success(order);
    }
    // 新增：更新订单状态
    @PostMapping("/update")
    public Result updateOrderStatus(@RequestBody PaymentNotification paymentNotification){
        Order order = orderService.getById(paymentNotification.getOrderId());
        order.setStatus(paymentNotification.getStatus());
        orderService.updateById(order);
        return Result.success("更新成功",null);
    }
}
